package com.zhy.learning.demo.service.impl;

import com.zhy.learning.demo.mapper.OrderInfoMapper;
import com.zhy.learning.demo.mapper.ProductMapper;
import com.zhy.learning.demo.mapper.UserMapper;
import com.zhy.learning.demo.pojo.OrderInfo;
import com.zhy.learning.demo.pojo.Product;
import com.zhy.learning.demo.pojo.User;
import com.zhy.learning.demo.service.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhy
 * @version 1.0.0
 * @ClassName DemoServiceImpl.java
 * @Description TODO
 * @createTime 2021年09月01日 21:30:00
 */
@Service
public class DemoServiceImpl implements DemoService {

    private final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    Lock lock = new ReentrantLock();

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Transactional
    public void testTransactional() {
        lock.lock();
        try {
            logger.info("{}:抢到锁啦，进入方法", Thread.currentThread().getName());
            User user = userMapper.findById(1);
            int age = user.getAge();
            logger.info("{}:当前age={}", Thread.currentThread().getName(), age);
            if (age > 0) {
                User updateUser = new User();
                updateUser.setId(user.getId());
                updateUser.setName(user.getName());
                updateUser.setAge(age - 1);
                userMapper.updateByUser(updateUser);
                User addUser = new User();
                addUser.setName(Thread.currentThread().getName());
                addUser.setNick(user.getName());
                userMapper.insert(addUser);
                logger.info("{}:减age，创建新用户完毕！", Thread.currentThread().getName());
            } else {
                logger.info("{}:age小于等于0", Thread.currentThread().getName());
            }
        } finally {
            logger.info("{}:释放锁啦，快来抢啊", Thread.currentThread().getName());
            lock.unlock();
        }
    }

    @Transactional
    public void sell() {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "：抢到锁啦，进入方法");
            Product product = productMapper.selectById(1);
            logger.info("product={}", product);
            Integer productCount = product.getProductCount();
            System.out.println(Thread.currentThread().getName() + "：当前库存=" + productCount);
            if (productCount > 0) {
                Product updateProduct = new Product();
                updateProduct.setId(product.getId());
                updateProduct.setProductCount(productCount - 1);
                productMapper.update(updateProduct);
                OrderInfo orderInfo = new OrderInfo();
                orderInfo.setBuyName(Thread.currentThread().getName());
                orderInfo.setBuyGoods(product.getProductName());
                orderInfoMapper.insert(orderInfo);
                System.out.println(Thread.currentThread().getName()  + "：减库存，创建订单完毕！");
            } else {
                System.out.println(Thread.currentThread().getName() + "：没库存啦！");
            }
        } finally {
            lock.unlock();
            System.out.println(Thread.currentThread().getName() + ":释放锁啦！");
        }

    }
}
